/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CtrRechMotCleGUI.java
 *
 * Created on 25 déc. 2008, 14:41:47
 */

package biblio;
import javax.swing.* ;
import java.awt.*;
import java.util.GregorianCalendar;
import java.awt.Color;
import java.lang.*;
import java.util.*;
/**
 *
 * @author ksz gardaud
 */
public class CtrRechMotCleGUI extends javax.swing.JDialog {

    private Bibliotheque biblio;
    private java.awt.Frame parent_frame;

    /** Creates new form CtrNouvExemplaireGUI */
    public CtrRechMotCleGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        parent_frame = parent;
        initComponents();
        this.setLocationRelativeTo(this.getParent());
    }

    public void menuPrincipal(Bibliotheque bib) {
        biblio = bib;
        Enumeration enumMotsCles = biblio.enumMotsCles();
        Vector<String> mesMotsCles = new Vector<String>();
        if (enumMotsCles.hasMoreElements()) {
            while (enumMotsCles.hasMoreElements()) {
                MotCle mc = (MotCle) enumMotsCles.nextElement();
                mesMotsCles.add(mc.motCle());
            }
        }
        motscles_list.setListData(mesMotsCles);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        motcle_label = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        motscles_list = new javax.swing.JList();
        verifier_button = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        motcle_notif_label = new javax.swing.JLabel();
        ouvrage_label = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        ouvrages_table = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        articles_table = new javax.swing.JTable();
        ouvrage_label1 = new javax.swing.JLabel();
        terminer_button = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Recherche par mot clé");

        motcle_label.setText("Mot clé :");

        motscles_list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(motscles_list);

        verifier_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/find.png"))); // NOI18N
        verifier_button.setText("Rechercher");
        verifier_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                verifier_buttonMousePressed(evt);
            }
        });

        motcle_notif_label.setText(" ");

        ouvrage_label.setText("Ouvrage(s) :");

        ouvrages_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ISBN", "Titre", "Editeur", "Date édition"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(ouvrages_table);
        ouvrages_table.getColumnModel().getColumn(0).setResizable(false);
        ouvrages_table.getColumnModel().getColumn(1).setResizable(false);
        ouvrages_table.getColumnModel().getColumn(2).setResizable(false);
        ouvrages_table.getColumnModel().getColumn(3).setResizable(false);

        articles_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Titre", "Page", "Parution", "Periodique"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(articles_table);
        articles_table.getColumnModel().getColumn(0).setResizable(false);
        articles_table.getColumnModel().getColumn(1).setResizable(false);
        articles_table.getColumnModel().getColumn(2).setResizable(false);
        articles_table.getColumnModel().getColumn(3).setResizable(false);

        ouvrage_label1.setText("Article(s) :");

        terminer_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/flag_red.png"))); // NOI18N
        terminer_button.setText("Fin");
        terminer_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                terminer_buttonMousePressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(motcle_label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(motcle_notif_label, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(verifier_button)
                        .addContainerGap())))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ouvrage_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ouvrage_label1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(404, Short.MAX_VALUE)
                .addComponent(terminer_button, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {motcle_label, ouvrage_label});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane2, jScrollPane3});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {terminer_button, verifier_button});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(motcle_label)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(motcle_notif_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(verifier_button)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ouvrage_label)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ouvrage_label1))
                .addGap(18, 18, 18)
                .addComponent(terminer_button)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void verifier_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_verifier_buttonMousePressed
        // TODO add your handling code here:
        motcle_notif_label.setText(" ");
        // on récupère la liste des mots clés selectionnées
        String mot_cle = (String) motscles_list.getSelectedValue();
        if(mot_cle == null){
            motcle_notif_label.setForeground(Color.red);
            motcle_notif_label.setText("Vous devez saisir un mot clé.");
        }else {
              MotCle mc = biblio.unMotCle(mot_cle);
              if(mc == null) {
                    motcle_notif_label.setForeground(Color.red);
                   motcle_notif_label.setText("Ce mot clé n'existe pas.");
              } else {
                  // on remplit les ouvrages
                  Set<Object> mesOuvrages = mc.tesOuvrages();
                  javax.swing.table.DefaultTableModel dtmOuvrages = new javax.swing.table.DefaultTableModel();
                  Vector<Vector<Object>> dataOuvrages = new Vector<Vector<Object>>();
                  dataOuvrages.clear();
                  Vector<String> columnOuvrages = new Vector<String>();
                  columnOuvrages.clear();
                  columnOuvrages.add("ISBN");
                  columnOuvrages.add("Titre");
                  columnOuvrages.add("Editeur");
                  columnOuvrages.add("Date édition");
                  int i =0;
                  for(Object ouvO:mesOuvrages){
                      i++;
                      Ouvrage ouv = (Ouvrage)ouvO;
                      Vector<Object> data_lineOuvrages = new Vector<Object>();
                      data_lineOuvrages.add(ouv.isbn());
                      data_lineOuvrages.add(ouv.titre());
                      data_lineOuvrages.add(ouv.editeur());
                      java.text.DateFormat dateFormat = new java.text.SimpleDateFormat("dd/MM/yyyy");
                      java.util.Date dateDate = ouv.dateEdition().getTime();
                      String dateString = dateFormat.format(dateDate);
                      data_lineOuvrages.add(dateString);
                      dataOuvrages.add(data_lineOuvrages);
                  }
                  dtmOuvrages.setDataVector(dataOuvrages, columnOuvrages);
                  ouvrages_table.setModel(dtmOuvrages);

                   // on remplit les articles
                  Set<Object> mesArticles = mc.tesArticles();
                  javax.swing.table.DefaultTableModel dtmArticles = new javax.swing.table.DefaultTableModel();
                  Vector<Vector<Object>> dataArticles = new Vector<Vector<Object>>();
                  dataArticles.clear();
                  Vector<String> columnArticles = new Vector<String>();
                  columnArticles.clear();
                  columnArticles.add("Titre");
                  columnArticles.add("Page");
                  columnArticles.add("Parution");
                  columnArticles.add("Periodique");
                  for(Object artA:mesArticles){
                      i++;
                      Article art = (Article)artA;
                      Vector<Object> data_lineArticles = new Vector<Object>();
                      data_lineArticles.add(art.titre());
                      Integer page = art.page();
                      data_lineArticles.add(page.toString());
                      data_lineArticles.add(art.taParution().idParution());
                      data_lineArticles.add(art.taParution().tonPeriodique().nom());
                      dataArticles.add(data_lineArticles);
                  }
                  dtmArticles.setDataVector(dataArticles, columnArticles);
                  articles_table.setModel(dtmArticles);
                  if(i==0){
                      motcle_notif_label.setForeground(Color.red);
                      motcle_notif_label.setText("Il n'y a aucun ouvrage et articles pour ce mot clé");
                  }
             }

        }
}//GEN-LAST:event_verifier_buttonMousePressed

    private void terminer_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_terminer_buttonMousePressed
        // TODO add your handling code here:
        // Vérification la non existence de l'ouvrage / ISBN
        this.dispose();
}//GEN-LAST:event_terminer_buttonMousePressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CtrRechMotCleGUI dialog = new CtrRechMotCleGUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable articles_table;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel motcle_label;
    private javax.swing.JLabel motcle_notif_label;
    private javax.swing.JList motscles_list;
    private javax.swing.JLabel ouvrage_label;
    private javax.swing.JLabel ouvrage_label1;
    private javax.swing.JTable ouvrages_table;
    private javax.swing.JButton terminer_button;
    private javax.swing.JButton verifier_button;
    // End of variables declaration//GEN-END:variables

}
